<?php
/*
 * 接口：/process_flow/approval
 * 说明：审批申请
 */

//关闭事物自动提交
$db->mysqlDB->falseAutoCommit();

//获取请求参数
$params = $route->bodyParams;

//更新当前审核状态
$P1 = array(
  "update_where" => "opf_id,opf_order_no",
  "opf_id" => $params['opf_id'],
  "opf_order_no" => $params['opf_order_no'],
  "opf_result" => $params['opf_result'],
  "opf_audit_time" => time(),
  "opf_reason" => $params['opf_reason'],
  "opf_state" => 2
);

$R1 = $db->mysqlDB->automaticUpdate($P1)->update("easyd_oa_process_flow");



//判断是同意还是拒绝，如果拒绝则将所有任务流更新为已完成，并更新申请状态，如果为同意则进行下一审核节点的更新
if($params['opf_result'] == '0'){
  
  //拒绝

  //发送通知
  $addNoticeParams = array(
    "on_receiver_id" => $params['opf_operator_id'],
    "on_content" => "您已 [拒绝] 了 [".$params['apply_nickname']."] 的 [".$params['apply_name']."] 申请",
    "on_create_time" => time()
  );
  $addNoticeRS = $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");

  //将所有后续任务全部更新为已完成，结束任务流
  $P2 = array(
    "update_where" => "opf_oapid",
    "opf_oapid"  => $params['opf_oapid'],
    "opf_result" => 0,
    "opf_state"  => 2
  );

  $R2 = $db->mysqlDB->automaticUpdate($P2)->update("easyd_oa_process_flow");

  //更新申请状态为已驳回
  $P3 = array(
    "update_where" => "oap_id",
    "oap_id" => $params['opf_oapid'],
    "oap_status" => 2
  );

  $R3 = $db->mysqlDB->automaticUpdate($P3)->update("easyd_oa_apply_process");

  //发送通知
  $addNoticeParams = array(
    "on_receiver_id" => $params['apply_uid'],
    "on_content" => "您的 [".$params['apply_name']."] 申请已被驳回，详情查询看申请信息",
    "on_create_time" => time()
  );
  $addNoticeRS = $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");


  if(!$R1 || !$R2 || !$R3){
    $db->mysqlDB->rollBackCommint();
    $db->mysqlDB->trueAutoCommit();
    $response->responseData(false);
  }else{
    $db->mysqlDB->actionCommint();
    $db->mysqlDB->trueAutoCommit();
    $response->responseData(true);
  }


}else{

  //同意

  //发送通知
  $addNoticeParams = array(
    "on_receiver_id" => $params['opf_operator_id'],
    "on_content" => "您已 [同意] 了 [".$params['apply_nickname']."] 的 [".$params['apply_name']."] 申请，该申请将继续向下一级发送",
    "on_create_time" => time()
  );
  $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");

  //判断是否为最后审核节点，如果是则更新申请状态，否则寻找下一审核节点，并更新成为正在审核状态
  if($params['opf_is_last'] == '0'){

 
    //获取下一节点顺序
    $nextOrder = (intval($params['opf_order_no'])+ 1);

    //更新下一个审核节点为正在处理
    $P2 = array(
      "update_where" => "opf_oapid,opf_order_no",
      "opf_oapid" => $params['opf_oapid'],
      "opf_order_no" => $nextOrder,
      "opf_state" => 1
    );

    $R2 = $db->mysqlDB->automaticUpdate($P2)->update("easyd_oa_process_flow");


    //查询所有下一节点用户，发送审批通知
    $searchNextNodeParams = array(
      "opf_oapid" => $params['opf_oapid'],
      "opf_order_no" => $nextOrder
    );

    $rsData = $db->mysqlDB->params($searchNextNodeParams)->select('easyd_oa_process_flow');

    //发送通知
    $addNoticeParams = array(
      "on_receiver_id" => $params['apply_uid'],
      "on_content" => "您的 [".$params['apply_name']."] 申请已被同意，该申请将继续向下一级发送",
      "on_create_time" => time()
    );
    $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");

    //循环进行通知的发送
    for($i=0;$i<count($rsData['data']);$i++){

        //发送通知
        $addNoticeParams = array(
          "on_receiver_id" => $rsData['data'][$i]['opf_operator_id'],
          "on_content" => "您有一个新的申请等待审批，请注意查看",
          "on_create_time" => time()
        );
        $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");

    }


  }else{

    //当前为最后一个审核节点，更新申请状态
    $P2 = array(
      "update_where" => "oap_id",
      "oap_id" => $params['opf_oapid'],
      "oap_status" => 1
    );

    $R2 = $db->mysqlDB->automaticUpdate($P2)->update("easyd_oa_apply_process");

    //发送通知
    $addNoticeParams = array(
      "on_receiver_id" => $params['apply_uid'],
      "on_content" => "您的 [".$params['apply_name']."] 申请已通过，详情查询看申请信息",
      "on_create_time" => time()
    );
    $addNoticeRS = $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");
  

  }


  if(!$R1 || !$R2){
    $db->mysqlDB->rollBackCommint();
    $db->mysqlDB->trueAutoCommit();
    $response->responseData(false);
  }else{
    $db->mysqlDB->actionCommint();
    $db->mysqlDB->trueAutoCommit();
    $response->responseData(true);
  }

}


?>